package com.example.Genericity;

import java.util.ArrayList;
import java.util.Arrays;

public class Test1 {
    public static void main(String[] args) {
        //method();
        //method2();
        ArrayList<String> list1 = addElement(new ArrayList<String>(), "a", "b", "c", "d");
        System.out.println(list1);  //[a, b, c, d]

        ArrayList<Integer> list2 = addElement(new ArrayList<Integer>(), 1, 2, 3, 4);
        System.out.println(list2);  //[1, 2, 3, 4]

    }

    public static <T> ArrayList<T> addElement(ArrayList<T> list, T t1, T t2, T t3, T t4) {
        list.add(t1);
        list.add(t2);
        list.add(t3);
        list.add(t4);
        return list;
    }

    private static void method2() {
        ArrayList<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        //将lis集合转成一个数组并返回
        //如果是空参，那么返回的数组类型为object类型
        Object[] objects = list.toArray();
        System.out.println(Arrays.toString(objects));//[aaa, bbb, ccc]
        String[] strings = list.toArray(new String[list.size()]);
        System.out.println(Arrays.toString(strings));//[aaa, bbb, ccc]
    }

    private static void method() {
        Box<String> b = new Box<>();
        b.setElement("给小刘的土味情话");
        String element = b.getElement();
        System.out.println(element);

        Box<Integer> b2 = new Box<>();
        b2.setElement(11);
        Integer element2 = b2.getElement();
        System.out.println(element2);
    }
}

class Box<E> {
    private E element;

    public E getElement() {
        return element;
    }

    public void setElement(E element) {
        this.element = element;
    }
}